#include "http_protocol_operation.h"

#include "./../http_protocol/http_protocol_parser.h"
#include "protocol_operation.h"

#include <cstdint>


std::shared_ptr<ProtocolOperation> HttpProtocolOperationFactory::CreateProtocolOperation()
{
    auto operation = new HttpProtocolOperation(HttpProtocolOperation::RESPONSE);
    return std::shared_ptr<ProtocolOperation>(operation);
}

std::shared_ptr<ProtocolOperation> HttpProtocolOperationFactoryForReq::CreateProtocolOperation()
{
    auto operation = new HttpProtocolOperation(HttpProtocolOperation::REQUEST);
    return std::shared_ptr<ProtocolOperation>(operation);
}

HttpProtocolOperation::HttpProtocolOperation(int mode)
{
    m_mode = mode;
    m_request_parser  = new HttpRequestProtocolParser();
    m_response_parser = new HttpResponseProtocolParser();
}

HttpProtocolOperation::~HttpProtocolOperation()
{
    delete m_request_parser;
    delete m_response_parser;
}

HttpRequest * HttpProtocolOperation::HttpRequestMsg() const
{
    return m_request_parser ? m_request_parser->Request() : nullptr;
}

HttpResponse * HttpProtocolOperation::HttpResponseMsg() const
{
    return m_response_parser ? m_response_parser->Response() : nullptr;
}

void HttpProtocolOperation::ClearMsg() const
{
    if (m_request_parser)
    {
        m_request_parser->Clear();
    }
    if (m_response_parser)
    {
        m_response_parser->Clear();
    }
}

int HttpProtocolOperation::ParserProtocol(const uint8_t * data, int data_length, int & use_length)
{
    int result = 0;
    if (m_mode == REQUEST)
    {
        result = m_request_parser->ParseHttpRequestProtocol(data, data_length, use_length);
        if (result == HttpRequestProtocolParser::NEED_DATA)
        {
            result = ProtocolOperation::OperationState::NEED_DATA;
        }
        else if (result == HttpRequestProtocolParser::FINISH)
        {
            result = ProtocolOperation::OperationState::FINISH;
        }
        else
        {
            result = ProtocolOperation::OperationState::BAD;
        }
    }
    else if (m_mode == RESPONSE)
    {
        result = m_response_parser->ParseHttpResponseProtocol(data, data_length, use_length);
        if (result == HttpResponseProtocolParser::NEED_DATA)
        {
            result = ProtocolOperation::OperationState::NEED_DATA;
            if ((m_response_parser->ParserStatus() == HttpRequestProtocolParser::PARSER_HEADER_SIGN_FINISH || m_response_parser->ParserStatus() == HttpRequestProtocolParser::PARSER_BODY)
                && m_request_parser->Request()->GetMethod() == "HEAD")
            {
                result = ProtocolOperation::OperationState::FINISH;
            }
        }
        else if (result == HttpResponseProtocolParser::FINISH)
        {
            result = ProtocolOperation::OperationState::FINISH;
        }
        else
        {
            result = ProtocolOperation::OperationState::BAD;
        }
    }
    else
    {
        result = HttpProtocolOperation::OperationState::BAD;
    }
    return result;
}

int HttpProtocolOperation::NeedNotFinishProcess()
{
    if (m_mode == RESPONSE)
    {
        printf("now response body size : %ld.\n", m_response_parser->Response()->Body().size());
        if (m_response_parser->Response()->Body().size() > 120000 && m_response_parser->ParserStatus() == HttpResponseProtocolParser::PARSER_BODY)
        {
            return false;
        }
    }
    return false;
}

